package edu.mit.media.fv.components
{
	import flash.display.*;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	
	public class FVTextField extends Sprite
	{
		private var _field:TextField = new TextField();
		private var _backwall:Sprite = new Sprite();
		private var _width:int = 100;
		private var _height:int = 28;
		private var _focusIn:Boolean = false;
		private var _mouseIn:Boolean = false;
		private var _format:TextFormat = new TextFormat();
		private var _glow:GlowFilter = new GlowFilter(0xFFFFFF,0.8,15,15);
		private var _etched:GlowFilter = new GlowFilter(0xFFFFFF,0.8,15,15,2,1,true,false);
		private var _bevel:BevelFilter = new BevelFilter(
			5, 45, 0x333333, 1, 0xFFFFFF, 1, 3, 3, 1, 1);
		private var defaultHeight:int = 0;
		
		public function FVTextField(x:int, y:int, width:int, height:int, text:String, defaultHeight:int=0){
			super();
			this.defaultHeight = defaultHeight;
			this.addChild(this._backwall);
			
			_format.align = TextFormatAlign.LEFT;
			_format.bold = true;
			_format.font = "Arial";
			_format.size = height*0.65;
			_format.color = 0x000000;
			
			_field.defaultTextFormat = _format;
			_field.type = TextFieldType.INPUT;
			_field.selectable = true;
			_field.text = text;
			_field.addEventListener(MouseEvent.ROLL_OVER, processMouseEntered);
			_field.addEventListener(MouseEvent.ROLL_OUT, processMouseExited);
			_field.addEventListener(FocusEvent.FOCUS_IN, processFocusGained);
			_field.addEventListener(FocusEvent.FOCUS_OUT, processFocusLost);
			this.addChild(_field);
			
			this.setBounds(x, y, width, height);
			
			this.filters = [this._bevel];
		}
		
		public function setBounds(x:int, y:int, width:int, height:int):void{
			this.x = x;
			this.y = y;
			this._width = width;
			this._height = height;
			this._field.width = width-height;
			this._field.height = height;
			this._field.x = height/2;
			if(defaultHeight >0){
				this._format.size = 20;
			}else{
				this._format.size = this._height*0.65;
			}
			this._field.defaultTextFormat = this._format;
			this.draw();
		}
		
		private function processFocusGained(event:FocusEvent):void{
			this._focusIn = true;
			this.filters = [this._glow, this._bevel];
			this.draw();
		}
		private function processFocusLost(event:FocusEvent):void{
			this._focusIn = false;
			this.filters = [this._bevel];
			this.draw();
		}
		private function processMouseExited(event:MouseEvent):void{
			this._mouseIn = false;
			this.draw();
		}
		private function processMouseEntered(event:MouseEvent):void{
			this._mouseIn = true;
			this.draw();
		}
		
		private function draw():void{
			this._backwall.graphics.clear();	
			
			if(this._focusIn){
				this._backwall.graphics.beginFill(0xFFFFFF, 1.0);//white
				//this._backwall.graphics.lineStyle(1, 0x000000, 1.0, true);//black borders
			}else if(this._mouseIn){
				this._backwall.graphics.beginFill(0xCCCCCC, 1.0);//light gray
				//this._backwall.graphics.lineStyle(1, 0xFFFFFF, 1.0, true); //blue borders
			}else{
				this._backwall.graphics.beginFill(0x999999, 1.0);//gray
				//this._backwall.graphics.lineStyle(1, 0xFFFFFF, 1.0, true);//white borders
			}
			this._backwall.graphics.beginFill(0xFFFFFF, 1.0);//white
			this._backwall.graphics.drawRoundRect(0,0,this._width, this._height,this._height,this._height);
			this._backwall.graphics.endFill();
		}
		

		
		public function get text():String{
			return this._field.text;
		}
		public function set text(txt:String):void{
			this._field.text = txt;
		}
		public function set wordWrap(o:Boolean):void{
			this._field.wordWrap = o;
		}
		public function set multiline(o:Boolean):void{
			this._field.multiline = o;
		}
		public function set mouseWheelEnabled(o:Boolean):void{
			this._field.mouseWheelEnabled = o;
		}
		public function set editable(o:Boolean):void{
			if(o){
				this._field.type = TextFieldType.DYNAMIC;
				//this._field.selectable = false;
			}else{
				this._field.type = TextFieldType.DYNAMIC;
				//this._field.selectable = false;
			}
		}
		public function setTextFormat(format:TextFormat):void{
			this._field.setTextFormat(format);
		}
		public function set displayAsPassword(o:Boolean):void{
			this._field.displayAsPassword = o;
		}
	}
}